package com.ybkj.daijia.server.event.listener.zhuanxian;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderRefuseEvent;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverRefuseRecord;
import com.ybkj.daijia.server.service.DriverRefuseRecordService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import com.ybkj.daijia.server.zhuanxian.Order;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class ZhuanxianOrderRefuseEventListener implements
    ApplicationListener<ZhuanxianOrderRefuseEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhuanxianOrderRefuseEventListener.class);

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ZhuanxianService zhuanxianService;

    @Autowired
    private DriverRefuseRecordService driverRefuseRecordService;

    @Override
    @Async
    public void onApplicationEvent(ZhuanxianOrderRefuseEvent event) {

        Long orderId = (Long) event.getSource();
        Long employId = event.getEmployId();
        String message = event.getMessage();

        Order order = zhuanxianService.findOne(orderId);

        Driver driver = driverService.findOne(employId);

        List<Order> activedList = zhuanxianService.findActivedOrdersByDriver(driver.getId());
        long pai = 0;
        long jie = 0;
        long busy = 0;
        if (!CollectionUtils.isEmpty(activedList)) {
            for (Order activedOrder : activedList) {
                if (!activedOrder.getId().equals(order.getId())) {
                    if (activedOrder.getStatus().equals(Order.ORDER_STATUS_PAIDAN)) {
                        ++pai;
                    } else if (activedOrder.getStatus().equals(Order.ORDER_STATUS_JIEDAN)) {
                        ++jie;
                    } else if (activedOrder.getStatus().equals(Order.ORDER_STATUS_ZHIXING)) {
                        ++busy;
                    }
                }
            }
        }
        if (busy > 0 || jie > 0) {
            if (driver.getStatus() != Driver.DRIVER_OFFLINE) {
                driverService
                    .updateStatusAndJobStatus(Driver.DRIVER_BUSY, Driver.DRIVER_JOBSTATUS_ZHUANXIAN,
                        driver.getId());
            }
        } else if (pai > 0) {
            if (driver.getStatus() != Driver.DRIVER_OFFLINE) {
                driverService.updateStatusAndJobStatus(Driver.DRIVER_APPLY_ORDER,
                    Driver.DRIVER_JOBSTATUS_ZHUANXIAN, driver.getId());
            }
        } else {
            if (driver.getStatus() != Driver.DRIVER_OFFLINE) {
                driverService.updateStatusAndJobStatus(Driver.DRIVER_ONLINE, null, driver.getId());
            }
        }

        //拒绝工单记录
        DriverRefuseRecord driverRefuseRecord = new DriverRefuseRecord();
        driverRefuseRecord.setEmployId(driver.getId());
        driverRefuseRecord.setEmployRealName(driver.getRealname());
        driverRefuseRecord.setEmployUserName(driver.getUsername());
        driverRefuseRecord.setEmployPhone(driver.getPhone());
        driverRefuseRecord.setHappend(new Date());
        driverRefuseRecord.setOrderId(order.getId());
        driverRefuseRecord.setOrderNumber(order.getOrderNo());
        driverRefuseRecord.setOrderCompanyId(order.getCompanyId());
        driverRefuseRecord.setMemo(message);
        driverRefuseRecord.setOrderType(DriverRefuseRecord.OrderType.zhuanxian);
        driverRefuseRecordService.save(driverRefuseRecord);

    }


}
